package com.example.asus.a222dzd_ordering.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;


import com.example.asus.a222dzd_ordering.R;
import com.example.asus.a222dzd_ordering.activity.FoodListActivity;
import com.example.asus.a222dzd_ordering.adapter.ShopAdapter;
import com.example.asus.a222dzd_ordering.base.BaseListner;
import com.example.asus.a222dzd_ordering.base.BaseModel;
import com.example.asus.a222dzd_ordering.bean.ShopBean;


import java.util.List;

public class ShopFragment extends Fragment{

    private RecyclerView recyclerView;
    private List<ShopBean> beans;
    private ShopAdapter adapter;

    public ShopFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_shop, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        initData(view);
        initEvent(view);
    }

    public void initView(View v) {
        //定义适配器对象
        adapter = new ShopAdapter(v.getContext());

        //需将布局中的RecyclerView对象的id指定为recyclerview
        recyclerView = v.findViewById(R.id.rv_shop);
        //定义线性⽅式显示数据
        LinearLayoutManager layoutManager =
                new LinearLayoutManager(getContext());
        //设置为位垂直⽅式显示（也可以⽔平⽅式显示）
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        //设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
        //设置适配器
        recyclerView.setAdapter(adapter);
    }

    BaseListner<List<ShopBean>> listner = new BaseListner<List<ShopBean>>() {
        @Override
        public void onResponse(List<ShopBean> shopBeans) {
            beans=shopBeans;
            if (beans==null)
                Toast.makeText(getContext(),"Kong", Toast.LENGTH_LONG).show();
            else{
                Toast.makeText(getContext(),"you"+beans.get(0).getShopname(), Toast.LENGTH_LONG).show();
                adapter.setData(beans);

            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(getContext(),"wrong", Toast.LENGTH_LONG).show();
        }
    };
    private void initData(View v){
        BaseModel<List<ShopBean>> model = new BaseModel<>();
        model.callEnqueue(model.service.getShop(),listner);

    }

    private void initEvent(View v){
        adapter.setOnItemClickListener(new ShopAdapter.OnItemClickListener() {
            @Override
            public void setItem(View view, int position) {
                Intent intent = new Intent(getContext(),FoodListActivity.class);
                intent.putExtra("shop_id",beans.get(position).getShop_id());
                startActivity(intent);
            }
        });
    }

}
